package mi.payment.ui.admin;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;

import lainey.ui.widget.TitleView;
import mi.payment.R;
import mi.payment.admin.MerchantManagementSettingsActivity;
import mi.payment.settings.MerchantSettingsOperation;
import mi.payment.settings.SecurityPasswordSettingsOperation;
import mi.payment.ui.base.ParentActivity;
import mi.payment.ui.base.PasswordKeyboard;

/**
 * 安全码输入页面
 */
public class ChangeInfoBySaveCodeActivity extends ParentActivity {

    private EditText mEditPassword;

    private int mIndex;
    private String mInfo;

    private String mSafeCode = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.admin_activity_merchant_safe_code);
        initView();
    }

    private void initView() {
        mInfo = getIntent().getStringExtra("merchantInfo");
        mIndex = getIntent().getIntExtra("merchantIndex", 0);

        TitleView titleView = findViewById(R.id.title_view);
        titleView.setCenterText(R.string.merchant_safe_code);
        titleView.setLeftImageOnClickListener(
                v -> onBackPressed()
        );


        mEditPassword = findViewById(R.id.edit_password);
        PasswordKeyboard passwordKeyboard = findViewById(R.id.passwordKeyboard);
        passwordKeyboard.setKeyClickCallback(
                new PasswordKeyboard.KeyClickCallback() {

                    @Override
                    public void onNumClick(int keyNum) {
                        if (mSafeCode.length() == 8) return;
                        mSafeCode += keyNum;
                        refreshPassword();
                        autoSkip(false);
                    }

                    @Override
                    public void onDelClick() {
                        int length = mSafeCode.length();
                        if (length > 0) {
                            mSafeCode = mSafeCode.substring(0, length - 1);
                            refreshPassword();
                        } else {
                            mSafeCode = "";
                            mEditPassword.setText("");
                        }
                    }

                    @Override
                    public void onEnterClick() {
                        autoSkip(true);
                    }

                }
        );
    }

    /**
     * 设置控件的监听事件
     */
    private void refreshPassword() {
        String var = "";
        for (int i = 0; i < mSafeCode.length(); i++) {
            var += "*";
        }
        mEditPassword.setText(var);
    }

    /**
     * 判断安全码输入正确后，自动跳转
     */
    private void autoSkip(boolean isToast) {
        String password = SecurityPasswordSettingsOperation.getInstance().getSecurityPassword();
        boolean equals = TextUtils.equals(password, mSafeCode);
        if (equals) {
            showToast(R.string.merchant_safe_code_success);
            if (mIndex == MerchantManagementSettingsActivity.MERCHANT) {
                MerchantSettingsOperation.getInstance().saveMerchantNum(mInfo);
            } else if (mIndex == MerchantManagementSettingsActivity.TERMINAL) {
                MerchantSettingsOperation.getInstance().saveTerminalNum(mInfo);
            }
            finish();
        } else {
            if (isToast) showToast(R.string.merchant_safe_code_failed);
        }
    }


}
